package com.sqnu.dashe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqnu.dashe.common.CustomException;
import com.sqnu.dashe.dto.SetmealDto;
import com.sqnu.dashe.entity.Dish;
import com.sqnu.dashe.entity.Setmeal;
import com.sqnu.dashe.entity.SetmealDish;
import com.sqnu.dashe.mapper.SetmealMapper;
import com.sqnu.dashe.service.SetmealDishService;
import com.sqnu.dashe.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author mmy
 * @create 2023-06-08 20:48
 * @description
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    @Transactional
    public void saveSetmeal(SetmealDto setmealDto) {

        //把setmealDto中的setmeal保存到setmeal中
        this.save(setmealDto);

        List<SetmealDish> list = setmealDto.getSetmealDishes();
        list.stream().map(item -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(list);
    }

    @Override
    @Transactional
    public SetmealDto getWithId(long setmealId) {
        SetmealDto setmealDto = new SetmealDto();
        Setmeal byId = this.getById(setmealId);
        BeanUtils.copyProperties(byId,setmealDto);
        List<SetmealDish> collect = setmealDishService.list().stream().map(item -> {
            item.setSetmealId(setmealId);
            return item;
        }).collect(Collectors.toList());
        setmealDto.setSetmealDishes(collect);
        return setmealDto;
    }



    /**
     * 修改套餐信息
     * @param setmealDto
     */
    @Override
    public void updateSetmeal(SetmealDto setmealDto) {
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        List<SetmealDish> list = setmealDto.getSetmealDishes();
        list.stream().map(item -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(list);

        this.updateById(setmealDto);
    }
    /**
     * 删除套餐信息
     * @param ids
     */
    @Override
    @Transactional
    public void myDeleteService(List<Long> ids) {
        //判断是否在售卖中，如果在售卖中，则抛出异常
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);
        int count = this.count(queryWrapper);
        if(count>0){
            throw new CustomException("套餐在售卖中，不能删除");
        }
        //删除套餐信息
        super.removeByIds(ids);
        //删除关系表中的数据
        //delete from setmeal_dish where setmeal_id in (ids);
        LambdaQueryWrapper<SetmealDish> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(queryWrapper1);
    }
}
